package com.rmi;

import com.dao.Flight;
import com.dao.FlightDaoImpl;
import com.dao.TransactionDaoImpl;
import com.dao.VacancyDaoImpl;
import com.util.Constant;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import java.rmi.RemoteException;
import java.util.List;

/**
 * Created by Phillip on 2014/4/15.
 */
public class RMIInterfaceImpl implements RMIInterface {

    private FlightDaoImpl flightDaoImpl;
    private TransactionDaoImpl transactionDaoImpl;
    private VacancyDaoImpl vacancyDaoImpl;
    private Logger logger;

    public RMIInterfaceImpl() {

        flightDaoImpl = new FlightDaoImpl();
        transactionDaoImpl = new TransactionDaoImpl();
        vacancyDaoImpl = new VacancyDaoImpl();

        logger = Logger.getLogger(RMIInterfaceImpl.class);
        PropertyConfigurator.configure(Constant.RMISER_PRO_STRING);
    }

    @Override
    public List<Flight> queryFlights(String deptString, String destString, String deptDateString, String returnDateString) throws RemoteException {
        logger.debug("RMIInterfaceImpl.queryFlights() is running");
        logger.debug("Request:");
        logger.debug("deptcity: " + deptString);
        logger.debug("destcity: " + destString);
        logger.debug("deptDate: " + deptDateString);
        logger.debug("returnDate: " + returnDateString);

        List<Flight> tempFlights1 = flightDaoImpl.queryByCityDate(deptString, destString, deptDateString, 1);
        List<Flight> tempFlights2 = flightDaoImpl.queryByCityDate(destString, deptString, returnDateString, 1);
        if (tempFlights1 == null || tempFlights2 == null) {
            logger.debug("1 or 2 flights have no vacancy");
            return null;
        }
        tempFlights1.addAll(tempFlights2);
        logger.debug("get tickets:" + tempFlights1.size()/2);
        for (int i = 0; i < tempFlights1.size(); i++) {
            logger.debug("flight number: " + tempFlights1.get(i).getFligthNOString());
        }

        return tempFlights1;
    }

    @Override
    public List<String> queryCity() throws RemoteException {
        logger.debug("RMIInterfaceImpl.queryCity is working, invoking flightDaoImpl.queryCityList...");
        logger.debug("Request: queryCity");
        logger.debug("result is :");
                List<String> cities = flightDaoImpl.queryCityList(1);
        for (int i = 0; i < cities.size(); i++) {
            logger.debug(cities.get(i));
        }
        return cities;
    }

    @Override
    public String book(String flightNoString1, String deptString, String clasString1, String flightNoString2, String returnString, String classString2, String nameString, String creditString) throws RemoteException {
        logger.debug("RMIInterfaceImpl.book() is working, checking transaction tables...");
        String transactionID1 = creditString + nameString + flightNoString1 + deptString;
        String transactionID2 = creditString + nameString + flightNoString2 + returnString;
        logger.debug("Request:");
        logger.debug(transactionID1);
        logger.debug(transactionID2);
        boolean b1 = transactionDaoImpl.queryTransaction(transactionID1, 1);
        boolean b2 = transactionDaoImpl.queryTransaction(transactionID2, 1);

        if (b1 && b2) {
            logger.debug("this user is clear to book both tickets, and adding records into transaction table...");
            transactionDaoImpl.insertTransaction(transactionID1, flightNoString1, deptString, nameString, 1);
            transactionDaoImpl.insertTransaction(transactionID2, flightNoString2, deptString, nameString, 1);
            logger.debug("invoking flightDaoImpl.bookByFlightDateClass()...");
            return vacancyDaoImpl.bookByFlightDateClass(flightNoString1, deptString, clasString1, flightNoString2, returnString, classString2, 1);
        } else if (!b1 || !b2) {
            logger.debug("this user has booked a ticket for the same flight before, return " + Constant.DUPLICATETICKETS_STRING);
            return Constant.DUPLICATETICKETS_STRING;
        }
        return Constant.BOOKFAILED_STRING;
    }

    @Override
    public String queryRates(String flightString, String classString) throws RemoteException {
        logger.debug("RMIInterfaceImpl.queryRates is running...");
        String tempString = flightDaoImpl.queryRates(flightString, classString, 1);
        logger.debug("and the price is " + tempString);
        return tempString;
    }

    @Override
    public String queryAirline(String deptString, String dsctString) throws RemoteException {
        logger.debug("RMIInterfaceImpl.queryAirline is running...");
        if (flightDaoImpl.isSupported(deptString, dsctString, 1)) {
            logger.debug("airline1 supports " + deptString + " - " + dsctString);
            return Constant.IS_SUPPORTED_STRING + "Air China";

        } else {
            logger.debug("airline1 does not support " + deptString + " - " + dsctString);
            return Constant.NOTSUPPORTED_STRING + "Air China";

        }
    }
}
